/****************************************************
          File Name:mq_send.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 20 Sep 2014 09:59:41 AM CST
*****************************************************/

#include"posix_ipc.h"
#define SIZE 1024

int main(int argc,char *argv[])
{
    mqd_t mq;
    int cnt;
    char buf[SIZE];

    if(argc<2 || strcmp(argv[1],"--help")==0)
    {
        printf("%s /mq\n",argv[0]);
        exit(EXIT_FAILURE);
    }
    if((mq=mq_open(argv[1],O_WRONLY))==(mqd_t)-1)
        err("open");

    for(;;)
    {
        cnt=read(STDIN_FILENO,buf,SIZE);
        if(cnt==0)
            break;
        else if(cnt==-1)
            err("read");
        if(mq_send(mq,buf,SIZE,0)==-1)
            err("mq_send");
    }

    if(errno)
        err("ERROR");
    exit(EXIT_SUCCESS);
}
